package com.servlet;

import com.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login03.do")
public class Login03Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if ("admin".equals(username) && "123".equals(password)) {
            User user = new User(username, password);
            //1.先放入session，再请求重定向 会进入过滤器 OK
//            request.getSession().setAttribute("user", user);
//            response.sendRedirect(request.getContextPath() + "/admin/admin.html");
            //2.先请求重定向，再放入session OK 不推荐
//            response.sendRedirect(request.getContextPath() + "/admin/admin.html");
//            request.getSession().setAttribute("user", user);
            //3.先放入session，再请求重定向。不会进入过滤器。
            request.getSession().setAttribute("user", user);
            request.getRequestDispatcher("/admin/admin.html").forward(request,response);
        }

    }


}
